package com.yzt.dynamicmodule.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;

import com.yzt.dynamicmodule.R;
import com.yzt.zhuju.utils.FastClickUtils;

import java.util.Timer;
import java.util.TimerTask;

public class CircleProgressButtonView1 extends LinearLayout {

    private static final int WHAT_LONG_CLICK = 1;

    private RoundProgressView roundProgressView;

    private ImageView imgCheck;

    private boolean isPressed = false;

    private final int mTime = 10;//录制最大时间s

    private final int mMinTime = 3;//录制最短时间

    private Timer timer;

    private long timeLong;

    public CircleProgressButtonView1(Context context) {
        super(context);
        initView(context);
    }

    public CircleProgressButtonView1(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context);
    }

    public CircleProgressButtonView1(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context);
    }

    private void initView(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_circle_pro, this, true);
        roundProgressView = findViewById(R.id.round_pro);
        imgCheck = findViewById(R.id.img_check);

        imgCheck.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                if (FastClickUtils.isFastClick()) {
                    return;
                }

                if (isPressed) {

                    if (timeLong < mMinTime) {
                        return;
                    }

                    isPressed = false;
                    Message mMessage = Message.obtain();
                    mMessage.what = WHAT_LONG_CLICK;
                    mHandler.sendMessage(mMessage);
                } else {
                    isPressed = true;
                    startTime();
                }
            }
        });
    }

    /**
     * 开始计时
     */
    private void startTime() {
        if (onClickListener != null) {
            onClickListener.onStartRecord();
        }
        imgCheck.setImageResource(R.mipmap.icon_video_record_check);
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        timer = new Timer();
        timeLong = -1;
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timeLong += 1;
                if (timeLong == mTime) {
                    if (onClickListener != null) {
                        onClickListener.onRecording(timeLong);
                    }
                    Message mMessage = Message.obtain();
                    mMessage.what = WHAT_LONG_CLICK;
                    mHandler.sendMessage(mMessage);
                }
                updateProgress();
            }
        }, 0, 1000);
    }

    private final Handler mHandler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == WHAT_LONG_CLICK) {
                stopTime();
            }
        }
    };

    /**
     * 停止计时
     */
    private void stopTime() {
        imgCheck.setImageResource(R.mipmap.icon_video_record);
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        if (onClickListener != null) {
            onClickListener.onEndRecord(timeLong);
        }
        clearData();
    }

    /**
     * 更新进度条
     */
    private void updateProgress() {
        roundProgressView.setProgress((int) (timeLong * 100 / mTime));
    }

    /**
     * 清除数据
     */
    public void clearData() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
        imgCheck.setImageResource(R.mipmap.icon_video_record);
        roundProgressView.setProgress(0);
    }

    public interface OnClickListener1 {

        void onStartRecord();

        void onRecording(long time);

        void onEndRecord(long time);
    }

    public OnClickListener1 onClickListener;

    public void setOnClickListener(OnClickListener1 onClickListener) {
        this.onClickListener = onClickListener;
    }
}